package com.tang.nacos.controller;

import com.tang.nacos.dao.OrderMapper;
import com.tang.nacos.dto.Order;
import com.tang.nacos.dto.User;
import com.tang.nacos.shardingdao.UserMapper;
import java.util.List;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author tanglingyu
 * @Date: 2021/04/06/ 15:29
 * @Description
 */
@RestController
public class TestController {

  @Autowired
  UserMapper userMapper;

  @Autowired
  OrderMapper orderMapper;

  // 查询非分库分表的数据
  @GetMapping("/test/order/get")
  public Order getOrder(Long id) {

    return orderMapper.selectByPrimaryKey(id);
  }

  // 根据分表键查询用户
  @GetMapping("/test/user")
  public User getUser(Long id) {

    return userMapper.selectByPrimaryKey(id);
  }

  // 根据非分表键查询数据
  @GetMapping("/test/user/age")
  public List<User> getUserByAge(Integer age) {

    return userMapper.select(User.builder().age(age).build());
  }

  // 添加新用户
  @GetMapping("/test/user/add")
  public String addUser() {
    User user = new User();
    user.setAge(99);
    user.setName("哈哈哈" + user.getAge());
    userMapper.insertSelective(user);
    return "success";
  }
}